<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>设备图像上传</title>
<style>
  body {
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    background: #f4f7fc;
    margin: 0;
    padding: 2rem;
    color: #333;
  }
  h1 {
    text-align: center;
    margin-bottom: 1.5rem;
    color: #2c3e50;
  }
  form {
    max-width: 500px;
    margin: 0 auto 2rem auto;
    padding: 1.5rem;
    background: white;
    border-radius: 8px;
    box-shadow: 0 0 15px rgb(0 0 0 / 0.1);
  }
  input[type="file"] {
    width: 100%;
    padding: 0.4rem;
    border: 1px solid #ccc;
    border-radius: 6px;
    margin-bottom: 1rem;
  }
  button {
    display: block;
    width: 100%;
    background-color: #3498db;
    color: white;
    font-size: 1rem;
    padding: 0.6rem;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.3s ease;
  }
  button:hover {
    background-color: #2980b9;
  }
  #result {
    max-width: 500px;
    margin: 0 auto;
    list-style: none;
    padding: 0;
  }
  #result li {
    background: white;
    padding: 0.8rem 1rem;
    margin-bottom: 0.6rem;
    border-radius: 6px;
    box-shadow: 0 0 8px rgb(0 0 0 / 0.05);
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  #result li.success {
    border-left: 5px solid #2ecc71;
  }
  #result li.error {
    border-left: 5px solid #e74c3c;
  }
  .filename {
    font-weight: 600;
  }
  .status {
    font-weight: 700;
  }
  .details {
    font-size: 0.9rem;
    color: #555;
    margin-left: 1rem;
    flex-grow: 1;
  }
</style>
</head>
<body>

<h1>设备图像批量上传</h1>

<form id="uploadForm" enctype="multipart/form-data">
  <input type="file" name="images" multiple accept=".jpg,.jpeg" required />
  <button type="submit">开始上传</button>
</form>

<ul id="result"></ul>

<script>
  const form = document.getElementById("uploadForm");
  const resultList = document.getElementById("result");

  form.addEventListener("submit", async (e) => {
    e.preventDefault();
    resultList.innerHTML = ""; // 清空结果列表

    const formData = new FormData(form);
    const files = form.elements["images"].files;

    if (files.length === 0) {
      alert("请至少选择一张图片上传");
      return;
    }

    // 遍历文件检查格式
    for (const file of files) {
      const ext = file.name.split('.').pop().toLowerCase();
      if (!["jpg", "jpeg"].includes(ext)) {
        const li = document.createElement("li");
        li.className = "error";
        li.innerHTML = `<span class="filename">${file.name}</span> - <span class="status">❌ 不支持的格式</span>`;
        resultList.appendChild(li);
        return; // 发现错误直接返回
      }
    }

    // 上传请求
    try {
      const response = await fetch("/upload", { method: "POST", body: formData });
      if (!response.ok) throw new Error("网络响应错误");
      const result = await response.json();

      result.forEach(item => {
        const li = document.createElement("li");
        li.className = item.status.startsWith("✅") ? "success" : "error";
        li.innerHTML = `
          <span class="filename">${item.filename}</span> -
          <span class="status">${item.status}</span>
          ${item.result ? `<span class="details">设备ID: ${item.result.image_id || '-'} </span>` : ''}
        `;
        resultList.appendChild(li);
      });

    } catch (err) {
      const li = document.createElement("li");
      li.className = "error";
      li.textContent = "上传失败，请检查网络或后端服务";
      resultList.appendChild(li);
    }
  });
</script>

</body>
</html>
